package com.kele.controller;

import com.kele.domain.Blog;
import com.kele.domain.Collections;
import com.kele.service.BlogService;
import com.kele.service.CollectionService;
import com.kele.utils.ResponseData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


@Controller
@RequestMapping("/collection")
public class CollectionController {
    @Resource
     private CollectionService collectionService;
    @Resource
     private BlogService blogService;

    @RequestMapping(value = "/Collection",method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value="博客id",name="blogid",required=true,type = "int"),
            @ApiImplicitParam(value="博客id",name="userid",required=true,type = "int")
    })
    public ResponseData Collection(int blogid,int userid){

        System.out.println("被收藏的文章id为："+blogid);
        System.out.println("执行收藏的用户id为："+userid);
        System.out.println("---------------------------------------->>>>>>>>>>>>>>>");
        //先查询是否存在收藏记录，然后再进行判断
        Collections cc=collectionService.findCollection(blogid,userid);
        if(cc!=null){
            System.out.println("找到点赞记录");
            System.out.println("collection中的收藏记录为：");
            System.out.println(cc);
            //删除收藏记录.
           int count= collectionService.deleteCollection(cc.getCollectionid());
            System.out.println("删除成功，删除的行数是"+count);
            Blog blog=blogService.findBlogById(cc.getBlogid());
            System.out.println("执行收藏-1的博客是：");
            System.out.println("blog");
            //操作数据库
            blogService.subCollection(blog.getBid());
            System.out.println("取消收藏成功");
            System.out.println("-------------------------<<<<<<");
            return ResponseData.success("取消收藏成功",blog);


        }else{
            System.out.println("没有找到收藏记录，进行添加收藏记录");
            Blog blog=blogService.findBlogById(blogid);
            System.out.println("执行收藏+1的博客为：");
            System.out.println(blog);
            Collections collection =new Collections();
            collection.setBlogid(blogid);
            collection.setUserid(userid);
            collection.setBlogname(blog.getTitle());
            collection.setBlogtype(blog.getType());
            //在collection中添加新的收藏记录
            Integer integer=collectionService.insertCollection(collection);
            System.out.println("修改记录"+integer);
            //操作数据库
            blogService.addCollection(blog.getBid());
            System.out.println("收藏成功");
            System.out.println("------------------------<<<<<<<");
            return ResponseData.success("收藏成功",blog) ;
        }

    }



}
